<template>
    <t-collapse :title="title[curretNode]">
        <ReceiptTable :columns="columnsOption[curretNode]" :tableData="tableData" :showNumber="true" v-if="!fullscreenShow">
            <template #[MAIN_FILE_FILENAME_A]="{ scope }">
                <span @click="handleClickReceiptInfo(scope, '01')" :class="[addStyle(scope, '01')]">{{
                    scope[MAIN_FILE_FILENAME_A] }}</span>
            </template>
            <template #[MAIN_FILE_FILENAME_B]="{ scope }">
                <span @click="handleClickReceiptInfo(scope, '02')" :class="[addStyle(scope, '02')]">{{
                    scope[MAIN_FILE_FILENAME_B]
                }}</span>
            </template>
            <template #[MAIN_FILE_FILENAME_C]="{ scope }">
                <span @click="handleClickReceiptInfo(scope, '03')" :class="[addStyle(scope, '03')]">{{
                    scope[MAIN_FILE_FILENAME_C]
                }}</span>
            </template>
            <template #[MAIN_FILE_FILENAME_D]="{ scope }">
                <span @click="handleClickReceiptInfo(scope, '04')" :class="[addStyle(scope, '04')]">{{
                    scope[MAIN_FILE_FILENAME_D]
                }}</span>
            </template>
            <template #[MAIN_FILE_FILENAME_E]="{ scope }">
                <span @click="handleClickReceiptInfo(scope, '05')" :class="[addStyle(scope)]">{{
                    scope[MAIN_FILE_FILENAME_E] }}</span>
            </template>
        </ReceiptTable>
        <pagination v-if="pageParam.total > 10" :total="pageParam.total" :page.sync="pageParam.pageNum"
            :limit.sync="pageParam.pageSize" @pagination="doSearch" />
        <!-- 表单弹出层 -->
        <el-dialog :title="dialogTitle" :visible.sync="showFormDialog" class="pms-zr-dialog" width="90%"
            :fullscreen="fullscreenShow"  :show-close="!fullscreenShow">
            <div style="max-height: 500px;overflow-y: auto;padding: 0 20px;">
                <ReceiptFrom :currentNode="curretNode" :currentType="tag" @checkVersionInfo="checkVersionInfo"
                    :showCall="showFormDialog" :dialogParams="dialogParams" :normalShow="normalShow" />
            </div>
        </el-dialog>
        <!--  整改问题清单-->
        <el-dialog title="整改问题清单" :visible.sync="specialDialogShow" class="pms-zr-dialog" width="90%"
            :before-close="closeDialog">
            <div style="max-height: 500px;overflow-y: auto;padding: 0 20px;">
                <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
                    <el-tab-pane v-for="item in tabsOption" :key="item.name" :label="item.label" :name="item.name"
                        :disabled="diableTabs[item.name]">
                    </el-tab-pane>
                </el-tabs>
                <ReceiptFrom v-if="['01', '04'].includes(activeName)" :currentNode="curretNode"
                    :currentType="activeName" :specalTag="tag" @checkVersionInfo="checkVersionInfo"
                    :showCall="specialDialogShow" :dialogParams="dialogParams" :rectifyShow="rectifyShow" />
                <div v-else v-loading="isLoading" element-loading-text="正在处理,请稍后">
                    <iframe :src="jumpUrl" scrolling="no" width="100%" frameborder="0" height="1000px" />
                </div>
            </div>
        </el-dialog>
        <!-- 版本记录 -->
        <el-dialog title="版本记录信息" :visible.sync="checkversionDialog" class="pms-zr-dialog" width="90%">
            <ReceiptTable :columns="versionRecordOption" :tableData="tableDataVersion" :showNumber="true"
                :showCall="checkversionDialog">
                <template #[MAIN_FILE_VERSION_NAME]="{ scope }">
                    <span @click="checkVersionClick(scope)" :class="[addStyle(scope)]">{{ scope[MAIN_FILE_VERSION_NAME]
                    }}</span>
                </template>
            </ReceiptTable>
        </el-dialog>

        <!-- 仅查看版本 -->
        <el-dialog :title="dialogTitle" :visible.sync="showCheckDialog" class="pms-zr-dialog" width="90%">
            <div style="max-height: 500px;overflow-y: auto;padding: 0 20px;">
                <ReceiptFrom :currentNode="curretNode" :currentType="checkTag" :onlyCheckShow="false"
                    :showCall="showCheckDialog" :dialogParams="versionDialogParams" :specalTag="tag" />
            </div>
        </el-dialog>
    </t-collapse>
</template>

<script>
import ReceiptTable from './components/receiptTable.vue'
import ReceiptFrom from './receiptForm/index.vue'
import SysConst from '@/common/const'
import {
    columnsOption,
    tabsOption,
    versionRecordOption
} from './option'
import {
    queryYsAndJwTableList,
    getMonitorAlarmVerifyDeviceUrl,
    getMonitorAlarmVerifyDeviceUrlB,
    getHandoverVersionInfo,
    getIsOpenRectification
} from '@/api/acceptance/index'
import { newOpenPostWindow } from '@/utils/newRequestCommonFormjs'
import {
    MAIN_FILE_FILENAME_A,
    MAIN_FILE_FILENAME_B,
    MAIN_FILE_FILENAME_C,
    MAIN_FILE_FILENAME_D,
    MAIN_FILE_FILENAME_E,
    MAIN_FILE_VERSION_NAME,
    MAIN_FILE_LINKA,
    MAIN_FILE_LINKB,
    MAIN_FILE_LINKC
} from './enumConfig'
export default {
    inject: ['prjDetail', 'workDetail'],
    name: "ReceiptInfo",
    components: {
        ReceiptTable,
        ReceiptFrom
    },
    props: {
        fullscreenShow: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        newPrjDetail() {
            return this.prjDetail()
        },
        newWorkDetail() {
            return this.workDetail()
        },
    },
    data() {
        return {
            title: {
                'F1': '项目验收单',
                'F12': '项目接维单'
            },
            tableData: [
            ],
            columnsOption,
            tabsOption,
            pageParam: {
                pageSize: SysConst.PAGESIZE, // 每页显示val条
                pageNum: SysConst.CURRENT_PAGE, // 当前页
                total: SysConst.TOTAL // 总数
            },
            showFormDialog: false,
            dialogTitle: '',
            specialDialogShow: false,
            activeName: '01',
            tag: '01',
            curretNode: 'F12',
            checkTag: '01',
            jumpUrl: '',
            versionRecordOption,
            checkversionDialog: false,
            tableDataVersion: [],
            showCheckDialog: false,
            MAIN_FILE_FILENAME_A,
            MAIN_FILE_FILENAME_B,
            MAIN_FILE_FILENAME_C,
            MAIN_FILE_FILENAME_D,
            MAIN_FILE_FILENAME_E,
            MAIN_FILE_VERSION_NAME,
            dialogParams: {},//弹框打开需要的参数
            saveCurrentRow: {},//保存当前行数据
            versionDialogParams: {},
            diableTabs: {
                '01': false,
                '02': false,
                '03': false,
                '04': false
            },
            rectifyNode: '',//RT1节点
            normalShow: false,
            rectifyShow: false,
            isLoading: true
        }
    },
    methods: {
        // 样式判断
        addStyle(row, type) {
            if (type === '01' && +row['sgLink'] === 0&&!this.fullscreenShow) {
                this.diableTabs[type] = true
                return ''
            }
            if (type === '02' && +row[MAIN_FILE_LINKA] === 0) {
                this.diableTabs[type] = true
                return ''
            }
            if (type === '03' && +row[MAIN_FILE_LINKB] === 0) {
                this.diableTabs[type] = true
                return ''
            }
            if (type === '04' && +row[MAIN_FILE_LINKC] === 0&&!this.fullscreenShow) {
                this.diableTabs[type] = true
                return ''
            }
            return 'up-color'
        },
        // 获取给打开弹框获取的参数
        getParams(row, type) {
            this.saveCurrentRow = row
            this.dialogParams = {
                prjId: row?.prjId,
                handoverType: type === '01' ? row?.sgCode : row?.wyCode,
            }
            this.getDiglogTitle(row, type)
        },
        getTabParams() {
            this.dialogParams.handoverType = this.activeName === '01' ? this.saveCurrentRow?.sgCode : this.saveCurrentRow?.wyCode
        },
        // 获取弹框标题
        getDiglogTitle(row, type) {
            let titles = ''
            switch (type) {
                case '01':
                    titles = row?.sgName
                    break;
                case '04':
                    titles = row?.wyName
                    break;
                case '05':
                    titles = row?.zgName
                    break;
                default:
                    break;
            }
            this.dialogTitle = titles
        },
        /**
         * 点击打开弹框
         * @param row 当前行数据
         * @param type 01,04打开表单 05整改问题清单 02,03跳转其它系统
         */
        async handleClickReceiptInfo(row, type) {
            this.tag = type
            // 不符合条件不允许执行逻辑
            if (this.addStyle(row, type) === '') {
                return
            }
            if (+row.stageH === 1) {
                return this.$message.error('该项目存在未结束的销项流程')
            }
            this.getParams(row, type)
            // 不满足条件 直接return 不允许进行操作
            if (['01', '04'].includes(type)) {
                this.showFormDialog = true
                this.normalShow = ['F1', 'F12'].includes(this.rectifyNode)
            }
            // 整改
            if (type === '05') {
                await this.getRectification(row)
            }
            await this.commonJump(type, row)
        },
        async commonJump(type, row, show = true) {
            const EnumType = {
                'F1': 1,
                'F12': 2,
                'RT1': 3,
            }
            const EnumParams = {
                'F1': 0,
                'F12': this.tag === '05' ? 1 : 0,
                'RT1': this.tag === '05' ? 0 : 1,
            }
            // 跳转其它系统
            if (type === "02") {
                const params = {
                    prjId: row?.prjId,
                    type: EnumType[this.rectifyNode],
                    solid: this.$route.query.type === 'todo' && EnumParams[this.rectifyNode]
                }
                const res = await getMonitorAlarmVerifyDeviceUrl(params)
                this.jumpUrl = res.data?.jumpUrl
                if (show) {
                    newOpenPostWindow(res.data?.jumpUrl, {}, res.data?.isNew)
                }
            }
            // 跳转其它系统
            if (type === "03") {
                const params = {
                    prjId: row?.prjId,
                    viewType: this.tag === '05' ? 'unqualifiedLegacy' : 'noCompleteRectify'
                }
                const result = await getMonitorAlarmVerifyDeviceUrlB(params)
                this.jumpUrl = result.data?.jumpUrl
                if (show) {
                    newOpenPostWindow(result.data?.jumpUrl, {}, result.data?.isNew)
                }
            }
            setTimeout(() => {
                this.isLoading = false
            }, 2000)
        },
        async getRectification(row) {
            if (this.rectifyNode === 'F12') {
                const { code } = await getIsOpenRectification({ prjId: row.prjId })
                if (+code !== 200) {
                    return
                }
            }
            this.specialDialogShow = true
            this.rectifyShow = ['RT1'].includes(this.rectifyNode)
            this.getTabParams()
        },
        getParamsInfo(item) {
            // 获取列表
            const tableParams = {
                prjId: this.newPrjDetail.prjId,
                ifSingleProject: this.newPrjDetail.manageMode !== '03',
                workItemId: this.newWorkDetail?.workItemID,
                comprehensiveQuery: this.$route.query.type == '1' ? '1' : '',
            }
            const currentCopeNode = this.newWorkDetail?.bizObject?.bizState === 'RT1' ? 'F12' : this.newWorkDetail?.bizObject?.bizState
            // 参数
            const params = {
                pageNum: item?.page || SysConst.CURRENT_PAGE,
                pageSize: item?.limit || SysConst.PAGESIZE,
                subPrjId: this.newPrjDetail.manageMode === '01' ? [this.newPrjDetail.prjId] : [],
                nodeId: JSON.stringify(this.newWorkDetail?.bizObject?.bizState) !== undefined ? currentCopeNode : 1,
                pojo: this.newPrjDetail.manageMode === '01' ? {} : tableParams
            }
            if (this.rectifyNode === 'RT1') {
                params.bizSeq = this.newWorkDetail?.bizObject?.bizSeq
            }
            return params
        },
        async doSearch(item) {
            const params = this.getParamsInfo(item)
            try {
                const { data } = await queryYsAndJwTableList(params)
                this.tableData = data?.rows || []
                if (this.fullscreenShow) {
                    this.openDialog()
                }
            } catch (error) {
                console.log(error);
            }
        },
        // 打开弹框页面
        async openDialog() {
            const { currentNode } = this.$route.query
            for (let i = 0; i < this.tableData.length; i++) {
                
                if (currentNode === '01') {
                    this.handleClickReceiptInfo(this.tableData[i], '01')
                }
                if (currentNode === '04') {
                    this.handleClickReceiptInfo(this.tableData[i], '04')
                }
            }
        },
        async handleTabClick(tab, event) {
            this.tag = '05'
            if (['02', '03'].includes(this.activeName)) {
                this.isLoading = true
                await this.commonJump(this.activeName, this.dialogParams, false)
            }
            this.getTabParams()
        },
        // 查看版本记录
        async checkVersionInfo() {
            if (this.tag === '05') {
                this.getTabParams()
            }
            this.checkversionDialog = true
            try {
                const resultParams = {
                    ...this.dialogParams,
                    operType: this.curretNode,
                    detailHandoverType: this.dialogParams.handoverType,
                    handoverType: '5'
                }
                const { data } = await getHandoverVersionInfo(this.tag === '05' ? resultParams : { ...this.dialogParams, operType: this.curretNode })
                this.tableDataVersion = data
            } catch (error) {
                console.log(error);

            }
        },
        // 打开查看版本的表单
        checkVersionClick(row) {
            if (this.tag === '05') {
                this.checkTag = this.activeName
                this.versionDialogParams = {
                    ...this.dialogParams,
                    versionId: row.versionId,
                    handoverType: this.tag === '05' ? '5' : this.dialogParams.handoverType,
                    detailHandoverType: this.dialogParams.handoverType
                }
            } else {
                this.versionDialogParams = {
                    ...this.dialogParams,
                    versionId: row.versionId,
                }
            }
            console.log(this.versionDialogParams, "=>>");
            this.showCheckDialog = true
        },
        // 关闭弹窗
        closeDialog() {
            this.specialDialogShow = false
            this.activeName = '01'
        },
        getWorkItemInfo() {
            this.curretNode = JSON.stringify(this.newWorkDetail?.bizObject?.bizState) !== undefined ? this.newWorkDetail?.bizObject?.bizState : 'F12'
            this.rectifyNode = this.curretNode
            this.curretNode = this.curretNode === 'RT1' ? 'F12' : this.curretNode
        },
    },
    created() {
        this.getWorkItemInfo()
        this.doSearch()
    },
}

</script>
<style scoped lang='scss'>
.up-color {
    color: #02A7F0;
    cursor: pointer;
}

input[aria-hidden=true] {
    display: none !important;
}

::v-deep .el-tabs__header {
    margin: 0 0 0 0;
}
</style>